package User;

import dao.UserMapper;
import entity.Address;
import entity.User;
import entity.User2;
import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.Logger;
import org.junit.Test;
import utils.MyBatisUtil;

import java.text.SimpleDateFormat;
import java.util.*;

public class UserMapperTest {

    private Logger logger=Logger.getLogger(UserMapperTest.class);
    @Test
    public void testUserList(){
        SqlSession sqlSession=null;
        List<User> list=new ArrayList<>();
        Map<String,String> map=new HashMap<>();
        try {
            sqlSession= MyBatisUtil.createSqlSession();
            User user=new User();
            user.setUserName("赵");
            user.setUserRole(3);

            map.put("name","赵");
            map.put("role","3");

            list=sqlSession.getMapper(UserMapper.class).getUserMap(map);
        }catch(Exception e){
            e.printStackTrace();
        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        for (User user:list){
            logger.debug("userCode : "+user.getUserCode()+" and userName: "+user.getUserName());
        }
    }

    @Test
    public void testGetUserList(){
        SqlSession sqlSession=null;
        List<User> list=new ArrayList<>();
        try {
            sqlSession= MyBatisUtil.createSqlSession();
            User user=new User();
            user.setUserName("赵");
            user.setUserRole(3);

            list=sqlSession.getMapper(UserMapper.class).getUserList(user);
        }catch(Exception e){
            e.printStackTrace();
        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        for (User user:list){
            logger.debug("userCode : "+user.getUserCode()+" and userName: "+user.getUserName()+
                    " adn userRole:"+user.getUserRole()+" and userRoleName: "+user.getUserRoleName()+
                    " and address: "+user.getAddress());
        }
    }

    //示例12
    @Test
    public void testAdd(){
        SqlSession sqlSession=null;
        int count=0;
        try {
            sqlSession= MyBatisUtil.createSqlSession();
            User user=new User();
            user.setUserCode("test001");
            user.setUserName("测试用户001");
            user.setUserPassword("1234567");
            Date birthday=new SimpleDateFormat("yyyy-MM-dd").parse("1984-12-12");
            user.setBirthday(birthday);
            user.setAddress("地址测试");
            user.setGender(1);
            user.setPhone("13688783697");
            user.setUserRole(1);
            user.setCreatedBy(1);
            user.setCreationDate(new Date());
            count=sqlSession.getMapper(UserMapper.class).add(user);
            int i=2/0;
            sqlSession.commit();
        }catch(Exception e){
            e.printStackTrace();
            sqlSession.rollback();
            count=0;
        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        logger.debug("testAdd count:"+count);
    }


    //示例14
    @Test
    public void testModify(){
        SqlSession sqlSession=null;
        int count=0;
        try {
            sqlSession= MyBatisUtil.createSqlSession();
            User user=new User();
            user.setId(18);
            user.setUserCode("testmodifty");
            user.setUserName("测试用户修改");
            user.setUserPassword("00000000");
            Date birthday=new SimpleDateFormat("yyyy-MM-dd").parse("1980-10-10");
            user.setBirthday(birthday);
            user.setAddress("地址测试修改");
            user.setGender(2);
            user.setPhone("13600000");
            user.setUserRole(2);
            user.setCreatedBy(1);
            user.setCreationDate(new Date());
            count=sqlSession.getMapper(UserMapper.class).modify(user);
            int i=2/0;
            sqlSession.commit();
        }catch(Exception e){
            e.printStackTrace();
            sqlSession.rollback();
            count=0;
        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        logger.debug("testAdd count:"+count);
    }

    //示例16
    @Test
    public void testupdatePwd(){
        SqlSession sqlSession=null;
        int count=0;
        String pwd="888888";
        Integer id=18;
        try {
            sqlSession= MyBatisUtil.createSqlSession();
            count=sqlSession.getMapper(UserMapper.class).updatePwd(id,pwd);
            sqlSession.commit();
        }catch(Exception e){
            e.printStackTrace();
            sqlSession.rollback();
            count=0;
        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        logger.debug("testAdd count:"+count);
    }

    //示例18
    @Test
    public void testupdateUserById(){
        SqlSession sqlSession=null;
        int count=0;
        Integer id=18;
        try {
            sqlSession= MyBatisUtil.createSqlSession();
            count=sqlSession.getMapper(UserMapper.class).deleteUserById(id);
            sqlSession.commit();
        }catch(Exception e){
            e.printStackTrace();
            sqlSession.rollback();
            count=0;
        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        logger.debug("testAdd count:"+count);
    }

    //示例21
    @Test
    public void testgetUserListByRoleId(){
        SqlSession sqlSession=null;
        List<User> list=new ArrayList<>();
        Integer roleId=3;
        try {
            sqlSession= MyBatisUtil.createSqlSession();
            list=sqlSession.getMapper(UserMapper.class).getUserListByRoleId(roleId);
        }catch(Exception e){
            e.printStackTrace();
        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        logger.debug("testAdd count:"+list.size());
        for (User user:list){
            logger.debug("userList========> userName"+user.getUserName()+ ",Role:"+user.getRole().getId()
                    +"---"+user.getRole().getRoleCode()+"---"+user.getRole().getRoleName());
        }
    }


    //示例25
    @Test
    public void testgetAddressListByUserId(){
        SqlSession sqlSession=null;
        List<User> list=new ArrayList<>();
        Integer userId=1;
        try {
            sqlSession= MyBatisUtil.createSqlSession();
            list=sqlSession.getMapper(UserMapper.class).getAddressListByUserId(userId);
        }catch(Exception e){
            e.printStackTrace();
        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        for (User user:list){
            logger.debug("userList========> userName"+user.getUserName()+ ",Role:"+user.getUserCode());

            for (Address address: user.getAddressList()){

                logger.debug("address -----> id:"+address.getId()+",contcat:"+address.getContact()
                +",addressDesc:"+address.getAddressDesc()+",tel:"+address.getTel());
            }
        }
    }

    //示例27
    @Test
    public void getAddressListByUserIdTest(){
        SqlSession sqlSession=null;
        List<User> list=new ArrayList<>();
        Integer userId=1;
        try {
            sqlSession= MyBatisUtil.createSqlSession();
            list=sqlSession.getMapper(UserMapper.class).getAddressListByUserId(userId);
        }catch(Exception e){
            e.printStackTrace();
        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        for (User user:list){
            if (user!=null){
                logger.debug("userList========> userName"+user.getUserName()+ ",Role:"+user.getUserCode()
                +",<未做映射字段>userPassword:"+user.getUserPassword());

                if (user.getAddressList().size()>0){
                    for (Address address: user.getAddressList()){

                        logger.debug("address -----> id:"+address.getId()+",contcat:"+address.getContact()
                                +",addressDesc:"+address.getAddressDesc()+",tel:"+address.getTel()
                                +",<未做映射字段>userId:"+address.getUserId());
                    }
                }else {
                    logger.debug("该用户下无地址列表");
                }
            }else {
                logger.debug("查无此用户");
            }

        }
    }

}
